package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelAppBusi;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelAppBusiRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowModelAppBusiInfoDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelAppBusiInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelAppBusiInfoExample;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Service - 模型与应用的关联表
 *
 * @author lilh
 * @date 2019-01-22 18:49
 */
@Service
public class FlowModelAppBusiRepositoryImpl implements FlowModelAppBusiRepository {

    @Autowired
    private FlowModelAppBusiInfoDAO flowModelAppBusiInfoDAO;

    @Override
    public Integer insert(FlowModelAppBusi entity) {
        FlowModelAppBusiInfo info = new FlowModelAppBusiInfo();
        BeanUtils.copyProperties(entity, info);
        flowModelAppBusiInfoDAO.insertSelective(info);
        return info.getId();
    }

    @Override
    public FlowModelAppBusi getAppByModelId(String modelId) {
        FlowModelAppBusiInfoExample modelAppBusiInfoExample = new FlowModelAppBusiInfoExample();
        modelAppBusiInfoExample.createCriteria().andModelIdEqualTo(modelId);
        List<FlowModelAppBusiInfo> modelAppBusiInfos = flowModelAppBusiInfoDAO.selectByExample(modelAppBusiInfoExample);
        if (CollectionUtils.isNotEmpty(modelAppBusiInfos)) {
            FlowModelAppBusiInfo entity = modelAppBusiInfos.get(0);
            if (Objects.nonNull(entity)) {
                FlowModelAppBusi result = new FlowModelAppBusi();
                BeanUtils.copyProperties(entity, result);
                return result;
            }
        }
        return null;
    }


    @Override
    public Integer update(FlowModelAppBusi modelAppBusiInfo) {
        FlowModelAppBusiInfo entity = new FlowModelAppBusiInfo();
        BeanUtils.copyProperties(modelAppBusiInfo, entity);
        return flowModelAppBusiInfoDAO.updateByPrimaryKeySelective(entity);
    }
}
